package com.yc.baseapi;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.URLUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * author: yc
 * description: ${description}
 * date: 2021/12/3 16:45
 */
public class TestOther {

    public static void main(String[] args) {
        TestOther to = new TestOther();
//        to.haveHistoryOrder(0, 2, 4);
//        to.haveHistoryOrder(2018, 2, 4);
//        to.haveHistoryOrder(2018, 0, 4);

//        System.out.println(to.checkPosition("120.072930,30.296008", "120.072930,30.296008"));
//        System.out.println(to.checkPosition("120.072930,30.296018", "120.072935,30.296008"));
//        System.out.println(to.checkPosition("120.072930,30.296218", "120.072935,30.296008"));
//        System.out.println(to.checkPosition("120.0729,30.296018", "120.072935,30.296008"));
//        System.out.println(to.checkPosition("120.072,30.296018", "120.072935,30.296008"));

//        to.m2();
//        to.m3();
//        to.m4();
        to.m5();
    }

    public void m5() {
        int i = 5;
//        System.out.println(i + '0');
//        System.out.println(i + "0");
        int j = 017;//这是八进制的15【8+7】
        int k = 11;
        int g = 0xa;//这是十六进制的10【a】
        System.out.println(j + k);
        System.out.println(g + k);
    }

    public void m4() {
        String str = "FBruB+tV/ff/oMZsB5wbhg==";
        System.out.println(URLUtil.encode(str));
        try {
            System.out.println(java.net.URLDecoder.decode(str, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public void m1() {
        Integer i = null;
        i = (1 > 2)?1:null;
    }

    public void m2() {
        Integer i = 2;
        Object o = null;
        o = i;
        System.out.println(o + "");
    }

    public void m3() {
        System.out.println(ArrayUtil.contains("100005427".split(","), 100005427+""));

        Integer[] arr = {100005427};
        System.out.println(ArrayUtil.contains(arr, 100005427));
    }


    //120.072930,30.296008 判断经纬度小数后4位是否一致
    private boolean checkPosition(String pos1, String pos2) {
        if (StringUtils.isEmpty(pos1) || StringUtils.isEmpty(pos2)) {
            return false;
        }
        if (pos1.equals(pos2)) {//完全一致
            return true;
        }

        String[] posArr1 = pos1.split(",");
        if (posArr1.length != 2) {
            return false;
        }
        String[] posArr2 = pos2.split(",");
        if (posArr2.length != 2) {
            return false;
        }

        if (posArr1[0].split("[.]")[1].length() < 4 || posArr1[1].split("[.]")[1].length() < 4 ||
                posArr2[0].split("[.]")[1].length() < 4 || posArr2[1].split("[.]")[1].length() < 4) {
            return false;//四位小数以内的数不一致：不一致
        }

        if (posArr1[0].substring(0, posArr1[0].indexOf(".") + 5).equals(posArr2[0].substring(0, posArr2[0].indexOf(".") + 5)) &&
                posArr1[1].substring(0, posArr1[1].indexOf(".") + 5).equals(posArr2[1].substring(0, posArr2[1].indexOf(".") + 5))) {
            return true;//四位小数一致：一致
        }

        return false;
    }

    /**
     * 十六进制串转化为byte数组
     *
     * @return the array of byte
     */
    public static byte[] hexToByte(String hex)
            throws IllegalArgumentException {
        if (hex.length() % 2 != 0) {
            throw new IllegalArgumentException();
        }
        char[] arr = hex.toCharArray();
        byte[] b = new byte[hex.length() / 2];
        for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
            String swap = "" + arr[i++] + arr[i];
            int byteint = Integer.parseInt(swap, 16) & 0xFF;
            b[j] = new Integer(byteint).byteValue();
        }
        return b;
    }

    public static byte[] fromHexString(String var0) {
        char[] var1 = var0.toUpperCase().toCharArray();
        int var2 = 0;

        for(int var3 = 0; var3 < var1.length; ++var3) {
            if (var1[var3] >= '0' && var1[var3] <= '9' || var1[var3] >= 'A' && var1[var3] <= 'F') {
                ++var2;
            }
        }

        byte[] var6 = new byte[var2 + 1 >> 1];
        int var4 = var2 & 1;

        for(int var5 = 0; var5 < var1.length; ++var5) {
            if (var1[var5] >= '0' && var1[var5] <= '9') {
                var6[var4 >> 1] = (byte)(var6[var4 >> 1] << 4);
                var6[var4 >> 1] = (byte)(var6[var4 >> 1] | var1[var5] - 48);
            } else {
                if (var1[var5] < 'A' || var1[var5] > 'F') {
                    continue;
                }

                var6[var4 >> 1] = (byte)(var6[var4 >> 1] << 4);
                var6[var4 >> 1] = (byte)(var6[var4 >> 1] | var1[var5] - 65 + 10);
            }

            ++var4;
        }

        return var6;
    }


    private Object[] haveHistoryOrder(int year, int month, int sysType) {
        boolean haveHistoryOrder = false;
        Date startDate, endDate;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String nowStr = "2021-04-01 12:12:12";
//        Date now = new Date();
        Date now = null;
        try {
            now = sdf.parse(nowStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int thisYear = DateUtil.year(now);

        if ((year < 2015 && year != 0) || year > thisYear) {
            throw new RuntimeException("年份异常:" + year);
        }
        if (month < 0 || month > 12) {
            throw new RuntimeException("月份异常:" + month);
        }

        int offSetMonth = -6;
        if (sysType != 4) {
            offSetMonth = -2;
        }


        if (year == 0) {//默认 x 月
            startDate = DateUtil.offsetMonth(now, offSetMonth);
            endDate = now;

            if (DateUtil.year(startDate) < thisYear) {
                haveHistoryOrder = true;
            }

        } else {
            if (year < thisYear) {
                haveHistoryOrder = true;
            }

            if (month == 0) {//全年
                startDate = DateUtil.beginOfMonth(DateUtil.parseDate(String.format("%d-01-01", year)));
                endDate = DateUtil.offsetMonth(startDate, 12);
            } else {//具体 某年 某月
                startDate = DateUtil.beginOfMonth(DateUtil.parseDate(String.format("%d-%d-01", year, month)));
                endDate = DateUtil.offsetMonth(startDate, 1);
            }
        }


        System.out.println(sdf.format(startDate) + " | " + sdf.format(endDate) + " | " + haveHistoryOrder);
        return new Object[]{startDate, endDate, haveHistoryOrder};
    }
}
